
import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/home/Login.vue'
import Home from '../components/home/home.vue'
import Welcome from '../components/home/welcome.vue'
import User from '../components/user/user.vue'
import Power from '../components/rights/power.vue'
import Roles from '../components/rights/roles.vue'
import GoodsCate from '../components/goods/goods_cate.vue'
import GoodsParams from '../components/goods/goods_params.vue'
import GoodsList from '../components/goods/goods_list.vue'
import addGoods from '../components/goods/addGoods.vue'
import Order from '../components/order/order.vue'
import Report from '../components/report/report.vue'

Vue.use(VueRouter)

const routes = [
  // 重定向
  { path: '/', redirect: '/login' },
  { path: '/login', component: Login },
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children: [
      {
        path: '/welcome',
        component: Welcome
      }, {
        path: '/users',
        component: User
      }, {
        path: '/rights',
        component: Power
      }, {
        path: '/roles',
        component: Roles
      },
      {
        path: '/categories',
        component: GoodsCate
      }, {
        path: '/params',
        component: GoodsParams
      },
      {
        path: '/goods',
        component: GoodsList
      },
      {
        path: '/addGoods',
        component: addGoods
      },
      {
        path: '/orders',
        component: Order
      },
      {
        path: '/reports',
        component: Report
      }
    ]
  }
]
const router = new VueRouter({
  mode: 'hash',
  routes
})

// 路由导航守卫登录拦截
router.beforeEach((to, from, next) => {
  // to即将要进入的目标
  // from当前导航正要离开的路由
  // 如果去login路由,就放行
  if (to.path === '/login') return next()
  const token = window.sessionStorage.getItem('token')
  // 如果token为空就进入login 不为空方行
  if (token === '') return next('/login')
  else next()
})

export default router
